package com.hd.trans.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.RelativeLayout
import androidx.databinding.DataBindingUtil
import com.hd.trans.R
import com.hd.trans.binding.visible
import com.hd.trans.databinding.ActivityText2VoiceBinding
import com.hd.trans.db.DataBaseMgr
import com.hd.trans.framework.click.SingleClick
import com.hd.trans.network.component.AiSparringListener
import com.hd.trans.network.component.AiSparringManager
import com.hd.trans.ui.base.BaseActivity
import com.hd.trans.utils.StringUtils
import com.hd.trans.utils.ToastUtils

@SuppressLint("SetTextI18n")
class Text2VoiceActivity: BaseActivity(), View.OnClickListener {
    private var currentVideoPath: String? = null
    private var consumeTime: Long = 0
    lateinit var mDataBinding: ActivityText2VoiceBinding
    private lateinit var mAiSparringManager: AiSparringManager
    private val filePathList = arrayListOf<String>()
    private var currentTagId = ""

    override fun getLayoutId(): Int {
        return R.layout.activity_text_2_voice
    }

    override fun initViewsAndEvents(savedInstanceState: Bundle?) {
        super.initViewsAndEvents(savedInstanceState)
        mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_text_2_voice)
        mDataBinding.click = this@Text2VoiceActivity
        mAiSparringManager = AiSparringManager()
        mAiSparringManager.getAiSparingConfig().type = "default"
        mAiSparringManager.getAiSparingConfig().engine = 1
        val en_woman = "https://oss-liuchengtu.hudunsoft.com/android_cloud_json/com.hudun.translation/voice/en_woman.MP3"
        val zh_woman = "https://oss-liuchengtu.hudunsoft.com/android_cloud_json/com.hudun.translation/voice/zh_woman.MP3"
        val en_man = "https://oss-liuchengtu.hudunsoft.com/android_cloud_json/com.hudun.translation/voice/en_man.mp3"
        val zh_man = "https://oss-liuchengtu.hudunsoft.com/android_cloud_json/com.hudun.translation/voice/zh_man.mp3"
        mAiSparringManager.getAiSparingConfig().voice = en_woman

        val content = "将进酒全文如下：\n\n君不见，黄河之水天上来，奔流到海不复回。\n君不见，高堂明镜悲白发，朝如青丝暮成雪。\n人生得意须尽欢，莫使金樽空对月。\n天生我材必有用，千金散尽还复来。\n烹羊宰牛且为乐，会须一饮三百杯。\n岑夫子，丹丘生，将进酒，杯莫停。\n与君歌一曲，请君为我倾耳听。\n钟鼓馔玉不足贵，但愿长醉不愿醒。\n古来圣贤皆寂寞，惟有饮者留其名。\n陈王昔时宴平乐，斗酒十千恣欢谑。\n主人何为言少钱，径须沽取对君酌。\n五花马，千金裘，呼儿将出换美酒，与尔同销万古愁。"
//        val content = "You are an AI speaking sparring assistant, you come to the UK and you will only answer in English"
//        val content = "您好，我是AI口语陪练，请问有什么可以帮到您吗？"
        //mDataBinding.etContent.setText("您好，我是AI口语陪练，请问有什么可以帮到您吗？")
        mDataBinding.etContent.setText(content)
        /*

                //相对布局 的布局参数
                val layoutParams = RelativeLayout.LayoutParams( //设置为填充父窗体
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.FILL_PARENT)
                //添加管理
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
                //设置布局
                mDataBinding.videoView.layoutParams = layoutParams
        */

        mDataBinding.videoView.setOnCompletionListener {
            playAudioFiles()
        }

        mAiSparringManager.setAiSparringListener(object : AiSparringListener{
            override fun onProgress(msg: String, progress: Int) {
                mXHandler.post {
                    mDataBinding.tvResult.text = msg
                }
            }

            override fun onSuccess(tagId: String, msg: String, filePath: String) {
                if(tagId == currentTagId){
                    filePathList.add(filePath)
                }else{
                    currentTagId = tagId
                    filePathList.clear()
                    filePathList.add(filePath)
                }
                mDataBinding.tvResult.text = "耗时："+((System.currentTimeMillis() - consumeTime)/1000).toString() + "秒"
                mDataBinding.btnText2Voice.text = "文字转语音"
                mDataBinding.videoView.visible = true
                //mDataBinding.btnStart.visible = true
                playAudioFiles()
            }

            override fun onFail(code: Int, msg: String) {
                mXHandler.post {
                    mDataBinding.tvResult.text = msg
                    mDataBinding.btnText2Voice.text = "文字转语音"
                }
            }
        })
    }
    private fun playAudioFiles(){
        if(filePathList.size == 0) return
        if(!mDataBinding.videoView.isPlaying){
            currentVideoPath = filePathList.removeAt(0)
            mDataBinding.videoView.resume()
            mDataBinding.videoView.setVideoPath(currentVideoPath)
            mDataBinding.videoView.start()
        }
    }

    @SingleClick
    override fun onClick(v: View?) {
        when(v) {
            mDataBinding.btnText2Voice -> {
                val content = mDataBinding.etContent.text.toString()
                if(content.isNullOrEmpty()) {
                    ToastUtils.showNormal("请输入内容")
                    return
                }
                consumeTime = System.currentTimeMillis()
                mDataBinding.btnText2Voice.text = "正在生成中..."
//                mAiSparringManager.createText2VoiceTask(content, true)
                mAiSparringManager.chatWithAI("How are you?")

                //mAiSparringManager.startAiSparring()
                //mAiSparringManager.createText2VoiceTask("您好，我是AI口语陪练，请问有什么可以帮到您吗？")
                //mAiSparringManager.testFFmpeg()

            }
            mDataBinding.btnStart -> {
                if(currentVideoPath?.isNotEmpty() == true && !mDataBinding.videoView.isPlaying){
                    mDataBinding.videoView.start()
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mAiSparringManager.onDestroy()
    }
}